<?php

#namespace Giocondo;

# container -- A widget which arrange their children in certain manner.
abstract class Container extends Widget {
	protected function cset($option, $value, $after_creation = TRUE) {
		global $Giocondo_wid_idx, $Giocondo_js;

		# Put most common attributes first to optimize.
		# TODO: if only a widget type uses a certain option, testing should be 
		# moved to its class by overriding this method there to optmize.
		switch ($option) {
			case 'children':
				# Box

				# Remove all existing children from the container.
				# TODO: because only numeric indexes are supported we should use:
				#for ($i = 0; $i < count($this->options['children']); $i++) {
				#	$child =& $this->options['children'][$i];
				foreach ($this->options['children'] as $child) {
					$child->has_parent = FALSE;
				}
				$this->options['children'] = array();
				$child_ids = array();

				# Place new children inside the container.
				foreach ((array)$value as $child) {
					# Check for unique parent.
					if ($child->has_parent) {
						Error::trigger(sprintf('window "w%u" has already a parent', $child->id), E_USER_ERROR);
					}
					$this->options['children'][] = $Giocondo_wid_idx[$child->id];
					$child->has_parent = TRUE;
					$child_ids[] = $child->id;
				}
				$Giocondo_js[$this->id][$option] = sprintf("c(%u,[%s],%u);", $this->id, implode(',', $child_ids), $this->options['orientation'] === 'vertical');
			break;

			case 'orientation':
				# Box

				if ($after_creation) {
					Error::trigger('Option "-orientation" and "-buttonType" can only set on creation', E_USER_ERROR);
				}
			break;

			case 'child':
				# ScrolledWindow, Window

				# Remove existing child from the container.
				if ($this->options['child'] !== '') {
					$this->options['child']->has_parent = FALSE;
				}

				# Place new child inside the container.
				# Check for unique parent.
				# TODO: what about empty values?
				if ($value->has_parent) {
					Error::trigger(sprintf('window "w%u" has already a parent', $value->id), E_USER_ERROR);
				}
				$this->options['child'] = $Giocondo_wid_idx[$value->id];
				$value->has_parent = TRUE;
				$Giocondo_js[$this->id][$option] = sprintf("c(%u,%u);", $this->id, $value->id);
			break;

			case 'label':
				# Box

				# TODO: when implementing the expander remember the label is a percent string
				$Giocondo_js[$this->id][$option] = sprintf("l(%u,'%s');", $this->id, String::filter($value));
			break;

			case 'title':
				# Window

				# TODO: test with a popup window
				$Giocondo_js[$this->id][$option] = sprintf("d('%s');", String::filter($value, 'j'));
			break;

			default:
				parent::cset($option, $value, $after_creation);
			return;
		}

		$this->options[$option] = $value;
	}

	public function delete() {
		parent::delete();

		# Deleting a container deletes all contained widgets too.
		# TODO: see note above about numeric index
		foreach ($this->options['children'] as $child) {
			# TODO: should not emit javascript code
			$child->delete();
		}
	}
}

# End of file
# vim: set ts=2 sw=2 noet:
?>
